package com.prince.web.controller;

import com.prince.aspect.AppLog;
import com.prince.model.Product;
import com.prince.page.Page;
import com.prince.util.JsonUtil;
import com.prince.util.PoiExcelExportUtil;
import com.prince.web.base.BaseController;
import com.prince.web.base.SessionUtil;
import com.prince.web.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述: 商品目录
 * @Author: 王培喜 58528792@163.com
 * @Date: 2021/4/6 14:27
 */
@Controller
@Validated
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    /**
     * 功能描述: 分页查询商品信息
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 15:25
     */
    @AppLog
    @PostMapping("/findProductByPage")
    public ResponseEntity<Map<String,Object>> findProductByPage(@RequestParam Map<String,Object> params) {
        Page page = this.getRequestPageRollInfo(params);
        Map<String,Object> map = productService.findProductByPage(params,page);
        return new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
    }

    /**
     * 功能描述: 添加商品信息
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/addProduct")
    public ResponseEntity<Map<String,Object>> addProduct(Product product) {
        product.setCreater(SessionUtil.getSession().getLoginName());
        Map<String,Object> map = productService.addProduct(product);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 修改商品信息
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/updateProduct")
    public ResponseEntity<Map<String,Object>> updateProduct(Product product) {
        product.setUpdater(SessionUtil.getSession().getLoginName());
        Map<String,Object> map = productService.updateProduct(product);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 删除商品信息
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/delProduct")
    public ResponseEntity<Map<String,Object>> delProduct(Product product) {
        product.setUpdater(SessionUtil.getSession().getLoginName());
        Map<String,Object> map = productService.delProduct(product);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 通过id查询商品
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @PostMapping("/findProductById")
    public ResponseEntity<Map<String,Object>> findProductById(@NotNull Integer id) {
        Map<String,Object> map = productService.findProductById(id);
        return new ResponseEntity<Map<String,Object>>(map,HttpStatus.OK);
    }
    /**
     * 功能描述: 通过拼音码查询商品
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @GetMapping("/findProductByPinyin")
    public void findProductByPinyin(@NotNull String pinyin, HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> list = productService.findProductByPinyin(pinyin);
        try {
            response.setContentType("text/plain");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("text/html; charset=utf-8");
            Map<String,String> map = new HashMap<String,String>();
            map.put("result", "content");
            PrintWriter out = response.getWriter();
            String jsonpCallback = request.getParameter("jsonpCallback");//客户端请求参数
            out.println(jsonpCallback+"("+ JsonUtil.toJSONString(list)+")");//返回jsonp格式数据
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能描述: 导出商品
     * @Author: 王培喜 58528792@163.com
     * @Date: 2021/4/6 16:02
     */
    @AppLog
    @GetMapping("/exportProduct")
    public void exportProduct(@RequestParam Map<String,Object> params,HttpServletResponse response){
        Map<String,Object> map = productService.findProductByPage(params,null);
        Object object = map.get("rows");
        if(null == object) {
            return;
        }
        List<Product> list = (List<Product>)object;
        //导出
        Date date = new Date();
        PoiExcelExportUtil poiExcelExportUtil = new PoiExcelExportUtil();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
        String strDate = simpleDateFormat.format(date);
        String[] titleColumns = {  "name","supplier","productUnit","specifications","purchasePrice","price", "pinyin","created"};
        String[] titleNames = {"商品名称","供应商", "单位","规格","零售价","进货价","拼音码", "时间"};
        //生成下载流
        poiExcelExportUtil.writeExcel(list, titleColumns, titleNames, response, "商品-" + strDate);

    }
}
